const MongodbC = require('mongodb').MongoClient;
var dburl = 'mongodb://47.104.5.86:3717/new_test';

module.exports = {
    handelQuery(collectionName) {
        return new Promise(function (resolve, reject) {
            MongodbC.connect(dburl, { useUnifiedTopology: true }, function (err, db) {
                if (err) {
                    throw err;
                }
                var dbo = db.db('new_test');
                dbo.collection(collectionName).find({}).toArray(function (err, result) { // 返回集合中所有数据
                    console.log(result);
                    resolve(result);
                    db.close();
                });

            });
        });
    },
    handleDelDocument(property) {
        return new Promise(function (resolve, reject) {
            MongodbC.connect(dburl, { useUnifiedTopology: true }, function (err, db) {
                if (err) {
                    throw err;
                }
                var dbo = db.db('new_test');
                console.log("1.1");
                dbo.collection("expressUnitPrice").deleteOne(property, function (err, result) { // 返回集合中所有数据
                    if (err) {
                        throw err;
                    }
                    console.log(result);
                    resolve(result);
                    db.close();
                });

            });
        });
    },
    handleQueryDistinctDocument(key) {
        return new Promise(function (resolve, reject) {
            MongodbC.connect(dburl, { useUnifiedTopology: true }, function (err, db) {
                if (err) {
                    throw err;
                }
                var dbo = db.db('new_test');
                console.log("1.1");
                // dbo.collection("expressUnitPrice").distinct({"key":key},function (err, result) { 
                dbo.collection("expressUnitPrice").distinct(key, function (err, result) {
                    if (err) {
                        throw err;
                    }
                    console.log(result);
                    resolve(result);
                    db.close();
                });

            });
        });
    },
    handleQueryDistinctDocument(key) {
        return new Promise(function (resolve, reject) {
            MongodbC.connect(dburl, { useUnifiedTopology: true }, function (err, db) {
                if (err) {
                    throw err;
                }
                var dbo = db.db('new_test');
                console.log("1.1");
                dbo.collection("expressUnitPrice").distinct({ "key": key }, function (err, result) {
                    // dbo.collection("expressUnitPrice").distinct(key, function (err, result) {
                    // dbo.collection("expressUnitPrice").aggregate([{}]), function (err, result) {
                    if (err) {
                        throw err;
                    }
                    console.log(result);
                    resolve(result);
                    db.close();
                });

            });
        });
    }
};